package com.taomu.app.zongchouhuli.moudle.launch.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.taomu.app.zongchouhuli.MainActivity;
import com.taomu.app.zongchouhuli.R;
import com.taomu.app.zongchouhuli.http.api.Request;
import com.taomu.app.zongchouhuli.moudle.launch.activity.WelComeActivity;
import com.taomu.app.zongchouhuli.moudle.login.activity.LoginActivity;
import com.taomu.app.zongchouhuli.utils.StorageHelper;

import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.Scheduler;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;


/**
 * 启动欢迎页面
 */
public class WelComFragment extends Fragment {

    private TextView mJumpTv;

    private View view;

    public static WelComFragment getInstence(){
        return new WelComFragment();
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_welcome,container,false);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
        initEvent();
        initData();
    }

    private void initView(){
        mJumpTv = view.findViewById(R.id.wel_jump_tv);
    }

    private void initEvent(){
        mJumpTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                junmpToMain();
            }
        });
    }

    private void initData(){
        startCountdown();
    }

    private Disposable timeDisposable;

    private Long time = 3l;

    private void startCountdown(){
        Observable.interval(1,TimeUnit.SECONDS)
                .map(new Function<Long, Long>() {
                    @Override
                    public Long apply(Long aLong) throws Exception {
                        return time - aLong;
                    }
                })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Long>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        timeDisposable = d;
                    }

                    @Override
                    public void onNext(Long aLong) {
                        if(aLong<=0){
                            junmpToMain();
                        }else {
                            mJumpTv.setText("跳过"+String.valueOf(aLong));
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    private boolean isJump = false;

    private void junmpToMain(){
        if(!isJump) {
            isJump = true;
            clearTimeDisposable();
            if(StorageHelper.isLogin()) {
                Intent intent = new Intent(getContext(), MainActivity.class);
                startActivity(intent);
                getActivity().finish();
            }else{
                Intent intent = new Intent(getContext(), LoginActivity.class);
                intent.putExtra("nologin", true);
                startActivity(intent);
                getActivity().finish();
            }
        }
    }

    private void clearTimeDisposable(){
        if(timeDisposable != null){
            timeDisposable.dispose();
            timeDisposable = null;
        }
    }

    @Override
    public void onDestroy() {
        clearTimeDisposable();
        super.onDestroy();
    }
}
